# Maintainer: Martell Malone <martellmalone@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=v8
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.3.66
pkgrel=1
pkgdesc="Fast and modern Javascript engine (mingw-w64)"
arch=('any')
url="https://code.google.com/p/v8"
license=("BSD")
makedepends=("gyp-svn")
depends=("${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-icu")
options=('!emptydirs' '!strip')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/v8/v8-git-mirror/archive/${pkgver}.tar.gz"
        "0000-Disable-USING_V8_SHARED-for-v8_snapshot-static-lib.patch"
        "0001-Include-win32-headers-h-before-undef-MemoryBarrier.patch"
        "0002-OS-TotalPhysicalMemory-needed-for-MinGW-w64-too.patch"
        "0003-Use-soname_version.dll-not-so.soname_version-on-Windows.patch"
        "0004-Create-an-import-library-on-Windows.patch"
        "0005-Use-gnuish-ccflags-for-gcc-on-Windows.patch"
        "0006-localtime_s-is-not-needed-for-mingw-w64.patch"
        "v8.pc")
sha256sums=('e9b96faa48d27c5a7f5694fc559cbdec67c954d44a8fbe3b6dd13cea4ca04553'
            '7c0857c7f6e9ab294ea0b9e13290e09684afb5fa1d52f947949e9f34167c4545'
            'd1687f7b5d20d0cfc3a86487d4078069f6ebb6f8deddb979054930f6f6e04efa'
            '83f113a8b97515dd402ca4a790fba127b8332dbd35475a0cb311e0f2196509bf'
            'a80566a3f3c394d60c45cc76b2e35f0757b624150e676abde67b1592623a3f2c'
            '726cbcae0219fea324b5215bf5287c0b5de7e8354899057a40b6c2b46d590b02'
            '14c0c03f5ad6cb8ff9c39841a03c088106a561983bada6e4fbb71e2494f05ad8'
            '024d8d96717d6c649528ea7bb03f3c3926221713942b852c4fbef6c9f5a840f6'
            '1b719bc6ac9a853fbf7ce47da63171aefa1a8b8556119edd5065d54cd927d1fd')

prepare() {
  cd "${srcdir}"/v8-${pkgver}
  patch -p1 -i ${srcdir}/0000-Disable-USING_V8_SHARED-for-v8_snapshot-static-lib.patch
  patch -p1 -i ${srcdir}/0001-Include-win32-headers-h-before-undef-MemoryBarrier.patch
#  patch -p1 -i ${srcdir}/0002-OS-TotalPhysicalMemory-needed-for-MinGW-w64-too.patch
  patch -p1 -i ${srcdir}/0003-Use-soname_version.dll-not-so.soname_version-on-Windows.patch
  patch -p1 -i ${srcdir}/0004-Create-an-import-library-on-Windows.patch
  patch -p1 -i ${srcdir}/0005-Use-gnuish-ccflags-for-gcc-on-Windows.patch
  patch -p1 -i ${srcdir}/0006-localtime_s-is-not-needed-for-mingw-w64.patch

  msg 'Fixing: python -> python2'
  #find -type f -exec sed -e 's_^#!/usr/bin/env python$_&2_' -e 's_^\(#!/usr/bin/env python2\).[45]$_\1_' -e 's_^#!/usr/bin/python$_&2_' -e "s_'python'_'python2'_" -i {} \;
  find test -type f -exec sed -e "s|python |python2 |" -i {} \;
  sed -i "s|python |python2 |" Makefile
  sed -i "s|python |python2 |" build/toolchain.gypi

  sed "s/@VERSION@/$pkgver/g" -i "$srcdir/v8.pc"
  sed "s|^prefix=.*|prefix=${MINGW_PREFIX}|g" -i "$srcdir/v8.pc"
}

build() {
  local BUILDTYPE=Release
  if check_option "debug" "y"; then
    BUILDTYPE="Debug"
  fi

  export PYTHON=/usr/bin/python2

  mkdir -p "${srcdir}/build-${CARCH}"

  case ${CARCH} in
    i686)
      _arch=ia32
    ;;
    x86_64)
      _arch=x64
    ;;
  esac
  cd "${srcdir}"/v8-${pkgver}

  GYP_GENERATORS=make \
    $PYTHON build/gyp_v8 \
      -Dv8_enable_i18n_support=true \
      -Duse_system_icu=1 \
      -Dconsole=readline \
      -Dcomponent=shared_library \
      -Dsoname_version=${pkgver} \
      -Dv8_target_arch=${_arch} \
      -Dgcc_version=49 \
      --generator-output=out \
      -f make

  LINK=g++ make -C out builddir="${PWD}/out/${BUILDTYPE}-${CARCH}" BUILDTYPE=${BUILDTYPE} mksnapshot V=1
  LINK=g++ make -C out builddir="${PWD}/out/${BUILDTYPE}-${CARCH}" BUILDTYPE=${BUILDTYPE} V=1
}

check() {
  cd "${_realname}-${pkgver}"
  local BUILDTYPE=Release
  if check_option "debug" "y"; then
    BUILDTYPE="Debug"
  fi
#  tools/run-tests.py --no-presubmit --outdir=out --buildbot --arch=$ARCH --mode=${BUILDTYPE} --progress=dots
}

package() {
  cd "${_realname}-${pkgver}"
  local BUILDTYPE=Release
  if check_option "debug" "y"; then
    BUILDTYPE="Debug"
  fi

  install -d "$pkgdir"/${MINGW_PREFIX}/bin
  install -Dm755 out/${BUILDTYPE}-${CARCH}/d8 "$pkgdir"/${MINGW_PREFIX}/bin/d8
  install -Dm755 out/${BUILDTYPE}-${CARCH}/lib.target/*.dll "$pkgdir"/${MINGW_PREFIX}/bin/

  install -d "$pkgdir"/${MINGW_PREFIX}/lib
  install -Dm755 out/${BUILDTYPE}-${CARCH}/*.a "$pkgdir"/${MINGW_PREFIX}/lib/

  install -d "$pkgdir"/${MINGW_PREFIX}/include
  install -Dm644 include/*.h "$pkgdir"/${MINGW_PREFIX}/include

  install -d "$pkgdir"/${MINGW_PREFIX}/lib/pkgconfig
  install -m644 "$srcdir/v8.pc" "$pkgdir"/${MINGW_PREFIX}/lib/pkgconfig/

  install -d "$pkgdir"/${MINGW_PREFIX}/share/licenses/v8
  install -m644 LICENSE* "$pkgdir"/${MINGW_PREFIX}/share/licenses/v8
}
